﻿<%@ Page Language="C#" ValidateRequest="false" %>

<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Xml.Linq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    XDocument items;
    NameValueCollection queryStrings;
    
    void Page_Init(object sender, EventArgs e)
    {
        queryStrings = Request.QueryString;
        items = XDocument.Load(MapPath(@"App_Data\Items.xml"));

        if (queryStrings.Count != 0)
            switch (queryStrings.Keys[0])
            {
                case "i":
                    QueryItem(queryStrings[0]);
                    break;
                default:
                    break;
            }
    }

    protected void btnSend_OnClick(object sender, EventArgs e)
    {
        if (queryStrings.Count == 0)
        {
            // New item
            items.Element("Items").AddFirst(new XElement("Item",
                new XAttribute("id", items.Descendants("Item").Count() + 1),
                new XElement("Title", tbTitle.Text),
                new XElement("Date", DateTime.Now),
                new XElement("Categories",
                    from c in tbCategories.Text.Split(new Char[] { ',' })
                    where c.Trim() != string.Empty
                    orderby c
                    select new XElement("Category", c)
                    ),
                new XElement("Content", tbContent.Text)
                ));
        }
        else
        {
            // Item update
            string date = items.Descendants("Item")
                .Where(i => i.Attribute("id").Value == queryStrings[0])
                .First().Element("Date").Value;
            items.Descendants("Item")
                .Where(i => i.Attribute("id").Value == queryStrings[0])
                .First().ReplaceWith(new XElement("Item",
                    new XAttribute("id", queryStrings[0]),
                    new XElement("Title", tbTitle.Text),
                    new XElement("Date", date),
                    new XElement("Categories",
                        from c in tbCategories.Text.Split(new Char[] { ',' })
                        where c.Trim() != string.Empty
                        orderby c
                        select new XElement("Category", c)
                        ),
                    new XElement("Content", tbContent.Text)
                    ));
        }

        // Hackish
        items.Save(MapPath(@"App_Data\Items.xml"));
    }

    void QueryItem(string id)
    {
        var query = from item in items.Descendants("Item")
                    where item.Attribute("id").Value == id
                    select new
                    {
                        Id = item.Attribute("id").Value,
                        Title = item.Element("Title").Value,
                        Date = item.Element("Date").Value,
                        Categories = (from category in item.Descendants("Category")
                                      orderby category.Value
                                      select category.Value).ToList(),
                        Content = item.Element("Content").Value
                    };
        tbTitle.Text = query.First().Title;
        
        foreach (string c in query.First().Categories)
            tbCategories.Text += c + ",";
        
        tbContent.Text = query.First().Content;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
    <script type="text/javascript">
        bkLib.onDomLoaded(function() {
            new nicEditor({fullPanel : true}).panelInstance('tbContent');
            });
    </script>
    <title><%=ConfigurationSettings.AppSettings["title"] %> > Writing Desk</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Title: <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox><br />
        Categories (comma separated values): <asp:TextBox ID="tbCategories" runat="server"></asp:TextBox><br />
        Content:<br />
        <asp:TextBox Columns="100" ID="tbContent" Rows="25" runat="server" TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="btnSend" OnClick="btnSend_OnClick" runat="server" Text="Go on-line!" />
    </div>
    </form>
</body>
</html>
